﻿using System;
using System.Drawing;
using System.Resources;
using AiFrame.InterfaceLib.MVP;
using AiFrame.InterfaceLib.Plugins;
using AiFrame.InterfaceLib.Windows.Controls;
using AiFrameDemo.Contacts.Core.DataStore;
using AiFrameDemo.Contacts.Core.ViewModel;
using AiFrameDemo.Contacts.Core.Views;
using AiFrameDemo.Lib.Model;

namespace AiFrameDemo.Contacts.Core
{
    public class ContactsController
    {
        private ICoreInterface _ci;

        public void Load(ICoreInterface ci, string programPath)
        {
            // Save the CoreInterface
            _ci = ci;
            
            // Initialize DaoFactory.
            DaoFactory.Initialize(ci.DatabaseConnection);

            // Initialize registry.
            ContactsRegistry.Instance.Initialize(ci, programPath);

            // Register factory
            ci.FactoryManager.RegisterFactory(ContactsFactory.ContactsViewFactory);

            // Register the contact class.
            ci.DatabaseConnection.AddMappingClass(typeof (Contact));

            ci.PluginManager.NewDatasetAssigned += PluginManager_NewDatasetAssigned;

            ResourceManager resourceManager = new ResourceManager("AiFrameDemo.Contacts.Core.Properties.Resources", this.GetType().Assembly);

            // Create a new data navigation bar.
            IDataNavigationBar dataNavigationBar = ci.NavigationBar.CreateDataNavigationBar();
            ci.NavigationBar.AddButton("contactsButton", "Contacts", "Show contact details", (Image) resourceManager.GetObject("contacts"), dataNavigationBar,
                                       OnNavBarItemClick);

            // Register menu items.
            object menuItem = ci.MainMenu.AddMainMenuItem("Contact details", "contactDataMenuItem", "Contact details",
                                                          (object)null, null);
            ci.MainMenu.AddMainMenuItem("menu element 1", "contactMi1", "menu element 1", menuItem,
                                        null);
            ci.MainMenu.AddMainMenuItem("menu element 2", "contactMi2", "menu element 2", menuItem,
                                        null);

        }

        private void PluginManager_NewDatasetAssigned(object sender, WindowChangedEventArgs e)
        {
            object wnd = e.Window;

            if (wnd is ISubWindowContainer<Customer> &&
                (wnd as ISubWindowContainer<Customer>).ID.Equals("EDIT_CUSTOMER"))
            {
                var container = (wnd as ISubWindowContainer<Customer>);
                IContactsListView contactsListView = ContactsFactory.ContactsViewFactory.CreateContactsListViewBase();
                var viewModel = new ContactsListViewModel(contactsListView);

                viewModel.SetAllocableDataFunction(
                    () =>
                    DaoFactory.Instance.ContactStore.FindByCustomerID(container.DataSourceProvider.GetDataSource().ID));
                viewModel.LoadDataObjects();

                container.GetWindowManager().RegisterWindow(contactsListView);
            }
        }

        private void OnNavBarItemClick(object sender, EventArgs e)
        {
            if (!_ci.WindowManager.ExistsWindow<IContactsListView>())
            {
                IContactsListView contactsListView = ContactsFactory.ContactsViewFactory.CreateContactsListView();
                var viewModel = new ContactsListViewModel(contactsListView);
                viewModel.LoadDataObjects();
                
                _ci.WindowManager.RegisterWindow(contactsListView);
            }
            else
                _ci.WindowManager.BringWindowToFront<IContactsListView>();
        }
    }
}